package com.ctgu.rabbitmq.rabbitmq.exchange.orderDemo;

import com.ctgu.rabbitmq.entity.OrdersEntity;
import com.ctgu.rabbitmq.service.UsersService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName MQOrderListener
 * @Author Alex2
 * @Date 2025/3/7 15:42
 **/
@Log4j2
@Component
public class RabbitMQPaymentConsumer {

    private final UsersService usersService;

    private final static String TAG = "Alex";

    public RabbitMQPaymentConsumer(UsersService usersService) {
        this.usersService = usersService;
    }

    @RabbitListener(queues = RabbitMQPaymentConfig.PAYMENT_QUEUE, errorHandler = "rabbitErrorHandler")
    public void receivePayment(OrdersEntity ordersEntity) {
        if (ordersEntity == null || ordersEntity.getUserId() == null) {
            log.debug("{} : 无效的订单信息 : {}", TAG, ordersEntity);
            return;
        }
        log.debug("{} : 消费了订单 : {}", TAG, ordersEntity);
        usersService.addRewardPoints(ordersEntity);
    }
}